package weektwo_day03_server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FTPServer {
	public static void main(String[] args) throws Exception {
		final String PRE_INPUT = "ftp/";
		ServerSocket server = new ServerSocket(8888);
		Socket socket = server.accept();
		System.out.println("continue....");
		InputStream in = socket.getInputStream();
		OutputStream out = socket.getOutputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String fileName = "";
		out.write("please input the command..\r\n".getBytes());
		while(true){
			fileName = reader.readLine();
			System.out.println(fileName);
			File file = new File(PRE_INPUT+fileName);
			
			out.write("200sucess\r\n".getBytes());
			
			OutputStream fileOutputStream = new FileOutputStream(file);
			byte[] b = new byte[4];
			int len = 0;
			while((len = in.read(b))!=-1){
				fileOutputStream.write(b,0,len);
			}
			fileOutputStream.close();
			reader.close();
			out.close();
			in.close();
			socket.close();
			server.close();
		}
	}
}
